package com.aisino.mplearning;

import java.sql.*;

/**
 * @author guochuantao
 * @version 1.0
 * @description
 * @since 2024/9/6 下午4:58
 */
public class ColumnTypeCheckerOracle {
    public static void main(String[] args) {
        String url = "jdbc:oracle:thin:@172.24.103.45:1521/orcl"; // 替换为你的数据库URL
        String user = "scott"; // 替换为你的数据库用户名
        String password = "Sjzl2024..."; // 替换为你的数据库密码
        String tableName = "xxl_job_log"; // 替换为你想要查询的表名
        try {
            Class.forName("oracle.jdbc.driver.OracleDriver");
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
        try (Connection conn = DriverManager.getConnection(url, user, password)) {
            DatabaseMetaData metaData = conn.getMetaData();
            ResultSet columns = metaData.getColumns(null, null, tableName, null);
            System.out.println("columns = " + columns);
            while (columns.next()) {
                String columnName = columns.getString("COLUMN_NAME");
                int columnType = columns.getInt("DATA_TYPE");
                final String typeName = columns.getString("TYPE_NAME");
                int len = columns.getInt("COLUMN_SIZE");
                final int dec = columns.getInt("DECIMAL_DIGITS");
                final String isnullable = columns.getString("IS_NULLABLE");
                final boolean nullable = isnullable == null || !isnullable.equals("NO");
                System.out.println("Column Name: " + columnName + ", =====>SQL Type: " + columnType);
                System.out.println("typeName = " + typeName + ", len = " + len + ", dec = " + dec + ", nullable = " + nullable);
                // System.out.println();
            }

        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}